package org.peterbjornx.graphics.jogl;

import java.awt.Point;
import javax.swing.*;
import java.awt.*;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseEvent;

/**
 * This class handles the mouse events. It extends the MouseInputAdapter Class.
 * The mouse invents are then used to zoom in/out, translate and rotate a 3D
 * scene
 * <p>
 */
public class MouseHandler extends MouseInputAdapter {


  
 
  private boolean mouseButtonDown = false;

  public Canvas thePanel;
    public AbstractJOGLRenderer myrenderer;


    /**
   * Checks if a mouse is clicked.
   *
   * @param e A mouse clicked event
   */
  public void mouseClicked(MouseEvent e) {
    thePanel.requestFocusInWindow();

    // If the RIGHT mouse button is clicked
    if (SwingUtilities.isRightMouseButton(e)) {
    
    }
  }

  /**
   * Checks if a mouse is pressed. A LEFT mouse button press signifies the
   * start of a drag process. The MIDDLE mouse button click signifies the
   * start of a translation as does a RIGHT mouse button click.
   *
   * @param e A mouse pressed event
   */
  public void mousePressed(MouseEvent e) {
    thePanel.requestFocusInWindow();

    if (SwingUtilities.isLeftMouseButton(e)) {
      mouseButtonDown = true;
    }

    // Update the scene - redraw the display following a mouse event
    //thePanel.drawabledisplay();
  }

  public void mouseReleased(MouseEvent e) {
    if (SwingUtilities.isLeftMouseButton(e)) {
      mouseButtonDown = false;
    }

    // Update the scene - redraw the display following a mouse event
    //thePanel.drawabledisplay();
  }

  public void mouseMoved(MouseEvent e) {
    Point mousePoint = e.getPoint();
    myrenderer.currentMousePoint.x = mousePoint.x;
    myrenderer.currentMousePoint.y = mousePoint.y;
    double matrotation[] = { 1.0, 0.0, 0.0, 0.0,
                             0.0, 1.0, 0.0, 0.0,
                             0.0, 0.0, 1.0, 0.0,
                             0.0, 0.0, 0.0, 1.0 };

    if (mouseButtonDown) {

      int xDiff = myrenderer.currentMousePoint.x - myrenderer.lastMousePoint.x;
      int yDiff = myrenderer.currentMousePoint.y - myrenderer.lastMousePoint.y;

      if (yDiff != 0) {
        matrotation = myrenderer.matRotate(matrotation, -yDiff / 3.0, new Vector3d(myrenderer.vRight));
        myrenderer.vLook = myrenderer.transformVector(matrotation, myrenderer.vLook);
        myrenderer.vUp = myrenderer.transformVector(matrotation, myrenderer.vUp);
      }
      
        if (xDiff != 0) {
          if (myrenderer.emulateFPS) {
            matrotation = myrenderer.matRotate(matrotation, -xDiff / 3.0, new Vector3d(0.0, 1.0, 0.0));
          } else {
            matrotation = myrenderer.matRotate(matrotation, -xDiff / 3.0, myrenderer.vUp);
          }          
          myrenderer.vLook = myrenderer.transformVector(matrotation, myrenderer.vLook);
          myrenderer.vUp = myrenderer.transformVector(matrotation, myrenderer.vUp);
          myrenderer.vRight = myrenderer.transformVector(matrotation, myrenderer.vRight);
        }

    }

    myrenderer.lastMousePoint.x = myrenderer.currentMousePoint.x;
    myrenderer.lastMousePoint.y = myrenderer.currentMousePoint.y;
  }

  /**
   * Checks if a mouse is dragged. A LEFT mouse button press signifies the
   * drag process. The MIDDLE mouse button click signifies the
   * start of a translation as does a RIGHT mouse button click.
   *
   * @param e A mouse pressed event
   */
  public void mouseDragged(MouseEvent e) {
    thePanel.requestFocusInWindow();

    if (SwingUtilities.isLeftMouseButton(e)) {
      Point mousePoint = e.getPoint();
      myrenderer.currentMousePoint.x = mousePoint.x;
      myrenderer.currentMousePoint.y = mousePoint.y;
      double matrotation[] = {1.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 0.0,
        0.0, 0.0, 1.0, 0.0,
        0.0, 0.0, 0.0, 1.0
      };

      if (mouseButtonDown) {

        int xDiff = myrenderer.currentMousePoint.x - myrenderer.lastMousePoint.x;
        int yDiff = myrenderer.currentMousePoint.y - myrenderer.lastMousePoint.y;

        if (yDiff != 0) {
          matrotation = myrenderer.matRotate(matrotation, -yDiff / 3.0, new Vector3d(myrenderer.vRight));
          myrenderer.vLook = myrenderer.transformVector(matrotation, myrenderer.vLook);
          myrenderer.vUp = myrenderer.transformVector(matrotation, myrenderer.vUp);
        }

        if (xDiff != 0) {
          if (myrenderer.emulateFPS) {
            matrotation = myrenderer.matRotate(matrotation, -xDiff / 3.0, new Vector3d(0.0, 1.0, 0.0));
          } else {
            matrotation = myrenderer.matRotate(matrotation, -xDiff / 3.0, myrenderer.vUp);
          } 
          myrenderer.vLook = myrenderer.transformVector(matrotation, myrenderer.vLook);
          myrenderer.vUp = myrenderer.transformVector(matrotation, myrenderer.vUp);
          myrenderer.vRight = myrenderer.transformVector(matrotation, myrenderer.vRight);
        }
      }

      myrenderer.lastMousePoint.x = myrenderer.currentMousePoint.x;
      myrenderer.lastMousePoint.y = myrenderer.currentMousePoint.y;
    }

    // Update the scene - redraw the display following a mouse event
    //thePanel.drawabledisplay();
  }
}